package com.situ.skymallimageapi.api;

import com.situ.service.ImageService;
import com.situ.skymallimageapi.util.AliOSSUtil;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "/api/v1/images", produces = "application/json;charset=utf-8")
public class ImageApi {
    /**
     * 上传图片
     * @param file 文件
     * @return 图片路径
     */
//    private ImageService imageService;
//
//    @Autowired
//    public void setUploadService(ImageService uploadService) {
//        this.imageService = uploadService;
//    }
//    @PostMapping("/upload")
//    public ResponseEntity<JsonResult<?>> upload(MultipartFile file) {
//        //此处可以选择将文件上传到本地，或阿里云，七牛云等等，本处演示上传到本地
//        String url = imageService.upload(file, "cover");
//
//        //todo:添加一个传入参数，用于控制图片自动删除行为
//
//        return ResponseEntity.ok(JsonResult.success(url));
//    }


    private AliOSSUtil aliOSSUtil;

    @Autowired
    public void setAliOSSUtil(AliOSSUtil aliOSSUtil) {
        this.aliOSSUtil = aliOSSUtil;
    }

    @PostMapping("/upload")
    public ResponseEntity<JsonResult<?>> upload(MultipartFile file) throws IOException {
        //上传到阿里云OSS
        String url = aliOSSUtil.upload(file, "cover");
        System.out.println("哈哈哈哈哈哈哈");
        return ResponseEntity.ok(JsonResult.success(url));
    }
}
